package com.gobo.servicebase.handler.exception;

import com.gobo.commonutils.ExceptionUtil;
import com.gobo.commonutils.R;
import com.gobo.servicebase.handler.exception.pojo.GoboException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     *
     * 全局异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R exception(Exception e){
        log.error(ExceptionUtil.getMessage(e));
        return R.exception();
    }

    /**
     * 特定异常处理
     * @param e
     * @return R
     */
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseBody
    public R illegalArgumentException(IllegalArgumentException e){
        log.error(ExceptionUtil.getMessage(e));
        return R.exception().message("非法参数异常");
    }

    @ExceptionHandler(GoboException.class)
    @ResponseBody
    public R goBoException(GoboException e){
        log.error(ExceptionUtil.getMessage(e));
        return R.exception().code(e.getCode()).message(e.getMsg());
    }



}
